package com.itheima.demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author chihb
 * @create-time 2024/7/2 10:34
 * @description
 */
public class Test1 {
    public static List<People> peoples=new ArrayList<>();

    public static void main(String[] args) {
        Random r=new Random();
        for (int i = 1; i <= 100; i++) {
            int code=r.nextInt(200)+1;
            if(isCanUse(code)){
                People p=new People(code,i);
                peoples.add(p);
            }else {
                i--;
            }
        }
        System.out.println("囚犯占位："+peoples);
        while (peoples.size()>1){
            List<People> tempPeoples=new ArrayList<>();
            for (int i = 1; i < peoples.size(); i+=2) {
                People p=peoples.get(i);
                tempPeoples.add(p);
            }
            peoples=tempPeoples;
        }
        People luckPeople=peoples.get(0);
        System.out.println("留下来得幸运儿是:"+luckPeople);
    }
    public static boolean isCanUse(int code){
        for (People people:peoples){
            if(people.getCode()==code) {
                return false;
            }
        }
        return true;
    }
}
